/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.flowable.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moon.admin.flowable.domain.FlowModelTypeQuery;
import com.moon.admin.flowable.entity.FlowModelTypeEntity;
import com.moon.admin.flowable.service.FlowModelTypeService;
import com.moon.admin.framework.mybatisplus.query.Condition;
import com.moon.admin.framework.secure.annotation.FormRepeat;
import com.moon.admin.framework.secure.annotation.PreAuth;
import com.moon.admin.framework.springmvc.domain.R;
import com.moon.admin.framework.syslog.annotation.SysLog;
import com.moon.admin.framework.util.ToolUtils;

import io.swagger.annotations.Api;

/**
 * 流程类型控制器
 * @author ninglong
 * @date 2020年2月11日
 */
@RestController
@RequestMapping("/flowable/modelType")
@Api(value = "流程类型管理",tags = "流程类型管理")
public class FlowableModelTypeController {
	

	@Autowired
	private FlowModelTypeService typeService;

	/**
	 * 分页查询
	 */
	@PostMapping("/list")
	@FormRepeat
	public R<IPage<FlowModelTypeEntity>> list(@RequestBody FlowModelTypeQuery flowType) {
		LambdaQueryWrapper<FlowModelTypeEntity> wrappers = Condition.getQueryWrapper(new FlowModelTypeEntity(), flowType);
		return R.success(typeService.page(Condition.getPage(flowType),wrappers));
	}


	@FormRepeat
	@SysLog(mod = "流程类型管理", fun = "新增修改流程类型")
	@PreAuth({"flowable_modelType_add","flowable_modelType_edit"})
	@PostMapping("/save")
	public R<Boolean> addSave(@RequestBody @Validated FlowModelTypeEntity flowType) {
		List<FlowModelTypeEntity> list = typeService.list(Wrappers.<FlowModelTypeEntity>lambdaQuery().select(FlowModelTypeEntity::getId).eq(FlowModelTypeEntity::getTypeName, flowType.getTypeName()));
		if(list.size()>1) {
			return R.fail((flowType.getId()==null?"新增":"修改")+"流程类型【" + flowType.getTypeName() + "】失败，流程类型名称已存在");
		}
		if(flowType.getId()==null && list.size()>0) {
			return R.fail("新增流程类型【" + flowType.getTypeName() + "】失败，流程类型名称已存在");
		}
		if(flowType.getId()!=null && list.size()==1 && list.get(0).getId().intValue()==flowType.getId().intValue()) {
			return R.fail("修改流程类型【" + flowType.getTypeName() + "】失败，流程类型名称已存在");
		}
		return R.status(typeService.saveOrUpdate(flowType));
	}
	
	@GetMapping(value = "removeByIds")
	@PreAuth("flowable_flowableType_remove")
	@SysLog(fun="流程类型管理",mod="删除流程类型")
	@FormRepeat
	public R<Void> removeByIds(String ids) {
		if(ToolUtils.isBlank(ids)) {
			return R.fail("请选择要删除的流程类型");
		}
		return R.status(typeService.removeByIds(ToolUtils.toIntList(ids)));
	}
}
